package com.kjlink.internal.workreport.user.controller;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import cn.xuqiudong.common.base.model.Remind;
import cn.xuqiudong.common.base.vo.Select2VO;
import com.kjlink.internal.workreport.base.controller.BaseController;
import com.kjlink.internal.workreport.base.enums.UserType;
import com.kjlink.internal.workreport.user.lookup.UserListLookup;
import com.kjlink.internal.workreport.user.model.User;
import com.kjlink.internal.workreport.user.service.UserService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * 功能: :用户,来自oa 控制层
 *
 * @author Vic.xu
 * @since 2024-04-29 13:38
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<UserService, User> {


    /**
     * 绑定一些通用的下拉框参数
     *
     * @param model
     */
    @ModelAttribute
    private void bindSelect(Model model) {
        model.addAttribute("types", UserType.values());
    }

    /**
     * 列表页
     */
    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView index(User lookup) {
        ModelAndView modelAndView = new ModelAndView("user/list");
        PageInfo<User> pager = service.page(lookup);
        pager.setLookup(lookup);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }


    /**
     * 前往新增页面
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id, RedirectAttributes attrs) {

        User entity = service.findById(id);
        if (entity == null) {
            attrs.addAttribute(Remind.danger().setMessage("不存在的User"));
            return new ModelAndView("redirect:/user/");
        }
        ModelAndView mv = new ModelAndView("user/form");
        mv.addObject("entity", entity);
        return mv;
    }


    @PostMapping("/save")
    public ModelAndView save(User user, RedirectAttributes attrs) {
        ModelAndView mv = new ModelAndView("redirect:/user/");
        service.save(user);
        attrs.addFlashAttribute(Remind.success().setMessage("保存成功"));
        return mv;
    }

    /**
     * 禁用
     */
    @GetMapping("/disable/{id}")
    public ModelAndView disable(@PathVariable Integer id, RedirectAttributes attrs) {
        updateEnable(id, false);
        return redirectHome(attrs);
    }

    /**
     * 启用
     */
    @GetMapping("/enable/{id}")
    public ModelAndView enable(@PathVariable Integer id, RedirectAttributes attrs) {
        updateEnable(id, true);
        return redirectHome(attrs);
    }

    private void updateEnable(Integer id, Boolean enable) {
        User user = new User();
        user.setId(id);
        user.setEnable(enable);
        service.update(user);
    }


    @Override
    protected String home() {
        return "/user/";
    }

    /**
     * 供项目选择的人员列表:  尚未被选择的其他人员
     */
    @RequestMapping("/list/{projectId}")
    public ModelAndView userList(@PathVariable("projectId") Integer projectId, UserListLookup lookup) {
        ModelAndView mv = new ModelAndView("user/user-list");
        lookup.setProjectId(projectId);
        List<User> datas = service.userList(lookup);
        mv.addObject("datas", datas);
        return mv;
    }

    /**
     * 人员下拉列表
     */
    @RequestMapping(value = "all")
    public BaseResponse<List<Select2VO>> all() {
        List<Select2VO> list = service.all();
        return BaseResponse.success(list);
    }
}
